package com.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.controller.model.NotificationDataModel;
import com.model.Notification;
import com.service.CollectMailService;
import com.service.MailingService;

@Component
@ManagedBean
@SessionScoped
public class MailingBean {

	@Autowired
	private MailingService mailingService;

	@Autowired
	private CollectMailService coollecterService;

	private boolean tableShown;

	private boolean groupByUser;

	private String login;

	private String password;

	private String smtp;

	private String port;

	{
		groupByUser = false;
		tableShown = false;
		login = "t.reporting.system@gmail.com";
		smtp = "smtp.gmail.com";
		port = "587";
	}

	// TODO: use properties instead of string fields
	// TODO: read it from mailing.properties
	private Properties properties;

	private List<Notification> list;

	private NotificationDataModel notificationDataModel = new NotificationDataModel(
			list);

	private Notification[] selectedNotifications;

	public void collectMailList() {
		setTableShown(true);
		list = coollecterService.getMailingList(isGroupByUser());
		notificationDataModel = new NotificationDataModel(list);
		Object[] objects = list.toArray();
		selectedNotifications = new Notification[list.size()];
		for (int i = 0; i < selectedNotifications.length; ++i) {
			selectedNotifications[i] = (Notification) objects[i];
		}
	}

	public void sendNotification() {
		mailingService.initializeSender(smtp, port);
		mailingService.sendNotifications(login, password,
				Arrays.asList(selectedNotifications));
		collectMailList();
	}

	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSmtp() {
		return smtp;
	}

	public void setSmtp(String smtp) {
		this.smtp = smtp;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public NotificationDataModel getNotificationDataModel() {
		return notificationDataModel;
	}

	public Notification[] getSelectedNotifications() {
		return selectedNotifications;
	}

	public void setSelectedNotifications(Notification[] selectedNotifications) {
		this.selectedNotifications = selectedNotifications;
	}

	public boolean isTableShown() {
		return tableShown;
	}

	public void setTableShown(boolean tableShown) {
		this.tableShown = tableShown;
	}

	public MailingService getMailingService() {
		return mailingService;
	}

	public void setMailingService(MailingService mailingService) {
		this.mailingService = mailingService;
	}

	public CollectMailService getCoollecterService() {
		return coollecterService;
	}

	public void setCoollecterService(CollectMailService coollecterService) {
		this.coollecterService = coollecterService;
	}

	public boolean isGroupByUser() {
		return groupByUser;
	}

	public void setGroupByUser(boolean groupByUser) {
		this.groupByUser = groupByUser;
	}

	public List<Notification> getList() {
		return list;
	}

	public void setList(List<Notification> list) {
		this.list = list;
	}

	public void setNotificationDataModel(
			NotificationDataModel notificationDataModel) {
		this.notificationDataModel = notificationDataModel;
	}

}
